package lxm.demo.ultra;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import lxm.demo.ultra.custom.WaveCustomPtrHeader;


public class WaveHeaderFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R2.id.ptr_wave_frame_layout)
    PtrFrameLayout ptrWaveFrameLayout;

    public static WaveHeaderFragment getInstance() {
        WaveHeaderFragment fragment = new WaveHeaderFragment();
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wave_header, container, false);
        unbinder = ButterKnife.bind(this, view);
        WaveCustomPtrHeader waveHeader = new WaveCustomPtrHeader(this.getContext());
        ptrWaveFrameLayout.setHeaderView(waveHeader);
        ptrWaveFrameLayout.addPtrUIHandler(waveHeader);
        ptrWaveFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return true;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                final WaveCustomPtrHeader waveHeader = (WaveCustomPtrHeader) frame.getHeaderView();
                waveHeader.setWaveProgress(0);
                waveHeader.invalidate();
//                waveHeader.setProgress(0);
//                invalidate();
                ptrWaveFrameLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        ptrWaveFrameLayout.autoRefresh();
//                        for (int i = 0; i <= 100; i++) {
//                            waveHeader.setWaveProgress(i);
//                            if (i == 100) {
//
//                            }
//                        }
                        ptrWaveFrameLayout.refreshComplete();
                    }

                }, 20000);
            }
        });
//        ptrWaveFrameLayout.setInterceptEventWhileWorking();

        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
